package com.rdc.dictionary.util;

import com.rdc.dictionary.bean.Word;
import com.rdc.dictionary.bean.WordSearch;

import org.litepal.crud.DataSupport;

import java.util.List;

/**
 * @author Soul Mate
 * @file 文件名
 * @brief 进行数据库操作的工具类
 */

public class DataBase
{
	/**
	* 保存生词到数据库
	*/
	public static void saveNewWord(Word word)
	{
		Word w = new Word();
		w.setWord(word.getWord());
		w.setE(word.getE());
		w.setMeans(word.getMeans());
		w.setA(word.getA());

//		w.setEnglishVoice(word.getEnglishVoice());
//		w.setAmericanVoice(word.getAmericanVoice());

		//获取本地mp3地址
		w.setAmericanVoice(getMP3Directory(word,"A"));
		w.setEnglishVoice(getMP3Directory(word,"E"));
		w.save();
	}


	/**
	 * 保存生词到数据库
	 */
	public static void saveWordSearchList(String search)
	{
		WordSearch w = new WordSearch();
		w.setName(search);
		w.save();
	}


	/**
	 * 检查数据库中是否存在生词
	 */
	public static boolean check(Word tmpWord)
	{
		List<Word> newWord = DataSupport.findAll(Word.class);
		for(Word word : newWord)
		{

			//增强for循环遍历查重
			if(word.getWord().equals(tmpWord.getWord()) )
			{
				return true;
			}
		}

		return false;
	}

	/**
	 * 检查数据库中是否存在记录
	 * 方法的重载
	 */
	public static boolean check(String search)
	{
		List<WordSearch> wordSearch = DataSupport.findAll(WordSearch.class);
		for(WordSearch w : wordSearch)
		{

			//增强for循环遍历查重
			if(w.getName().equals(search) )
			{
				return true;
			}
		}

		return false;
	}

	/**
	* 获取本地mp3的地址
	* @param word 单词数据
	* @return  mp3的本地地址
	*/
	public static String getMP3Directory(Word word, String ps)
	{
		String fileName = word.getWord() + "/" + ps + ".mp3";
		String address = FileUtil.getInstance().getPath(fileName);

		return address;

	}




}
